import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/res_partner_odoo_api.dart';
import 'package:flutter_admin/models/odoo/res_partner.dart';
import 'package:flutter_admin/pages/common/cry/cry_all.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/odoo/material/user_list.dart';
import '../../../generated/l10n.dart';

class ResPartnerEdit extends StatefulWidget {
  ResPartnerEdit({this.resPartner});

  final ResPartner? resPartner;

  @override
  _DmsDocumenttypeEditState createState() => _DmsDocumenttypeEditState();
}

class _DmsDocumenttypeEditState extends State<ResPartnerEdit> {
  late ResPartner resPartner;
  bool? isAdd;

  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  List<SelectOptionVO> customerNatureList = [];
  List<SelectOptionVO> customerLevelList = [];
  List<SelectOptionVO> customerTypeList = [];
  List<SelectOptionVO> customerStateList = [];
  List<SelectOptionVO> customerSourceList = [];
  List<SelectOptionVO> industryIdList = [];
  List<SelectOptionVO> creditRatingList = [];
  List<SelectOptionVO> countryIdList = [];
  @override
  void initState() {
    super.initState();
    this.isAdd = widget.resPartner == null;
    this.resPartner = widget.resPartner ?? ResPartner();
    _loadSelectVO();
  }

  _loadSelectVO() async {
    customerNatureList = [
      SelectOptionVO(value: '', label: ""),
      SelectOptionVO(value: '代理客户', label: "代理客户"),
      SelectOptionVO(value: '最终客户', label: "最终客户"),
    ];
    customerLevelList = [
      SelectOptionVO(value: '', label: ""),
      SelectOptionVO(value: '一般客户', label: "一般客户"),
      SelectOptionVO(value: '重要客户', label: "重要客户"),
    ];
    customerTypeList = [
      SelectOptionVO(value: '', label: ""),
      SelectOptionVO(value: '分销', label: "分销"),
      SelectOptionVO(value: '业主', label: "业主"),
      SelectOptionVO(value: '通信总包商', label: "通信总包商"),
    ];
    customerStateList = [
      SelectOptionVO(value: '', label: ""),
      SelectOptionVO(value: '合作', label: "合作"),
    ];
    customerSourceList = [
      SelectOptionVO(value: '', label: ""),
      SelectOptionVO(value: '自主开发', label: "自主开发"),
    ];
    industryIdList = await DynamicDataApi.getModelSelect(
      model: "res.partner.industry",
      name: "name",
    );
    creditRatingList = [
      SelectOptionVO(value: '', label: ""),
      SelectOptionVO(value: '一般', label: "一般"),
      SelectOptionVO(value: '良好', label: "良好"),
    ];
    countryIdList = await DynamicDataApi.getModelSelect(
      active: false,
      model: "res.country",
      name: "name",
    );
    setState(() {});
  }

  selectUser() async {
    formKey.currentState?.save();
    showDialog(
      barrierDismissible: false,
      context: context,
      builder: (BuildContext context) => Dialog(
        child: UserList(),
      ),
    ).then((v) {
      if (v != null) {
        formKey.currentState!.save();
        resPartner.userId = v['user_id'][0].toString();
        resPartner.userName = v['user_id'][1].toString();
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CrySelect(
            width: 300,
            label: '*客户性质',
            dataList: customerNatureList,
            value: resPartner.customerNature,
            onSaved: (v) {
              resPartner.customerNature = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            width: 300,
            label: '*客户名称',
            value: resPartner.name,
            onSaved: (v) {
              resPartner.name = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CrySelect(
            width: 300,
            label: '*客户等级',
            dataList: customerLevelList,
            value: resPartner.customerLevel,
            onSaved: (v) {
              resPartner.customerLevel = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CrySelect(
            width: 300,
            label: '*客户类型',
            dataList: customerTypeList,
            value: resPartner.customerType,
            onSaved: (v) {
              resPartner.customerType = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CrySelect(
            width: 300,
            label: '*客户状态',
            dataList: customerStateList,
            value: resPartner.customerState,
            onSaved: (v) {
              resPartner.customerState = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CrySelect(
            width: 300,
            label: '*客户来源',
            dataList: customerSourceList,
            value: resPartner.customerSource,
            onSaved: (v) {
              resPartner.customerSource = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CrySelect(
            width: 300,
            label: '*行业类型',
            dataList: industryIdList,
            value: resPartner.industryId,
            onSaved: (v) {
              resPartner.industryId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CrySelect(
            width: 300,
            label: '*信用等级',
            dataList: creditRatingList,
            value: resPartner.creditRating,
            onSaved: (v) {
              resPartner.creditRating = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            width: 300,
            label: '*主营业务',
            value: resPartner.mainBusiness,
            onSaved: (v) {
              resPartner.mainBusiness = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            width: 300,
            label: '公司背景',
            value: resPartner.companyBackground,
            onSaved: (v) {
              resPartner.companyBackground = v;
            },
          ),
          CryInput(
            width: 300,
            label: '主要联系人',
            value: resPartner.primaryContact,
            onSaved: (v) {
              resPartner.primaryContact = v;
            },
          ),
          CryInput(
            width: 300,
            label: 'E-MAIL',
            value: resPartner.email,
            onSaved: (v) {
              resPartner.email = v;
            },
          ),
          CryInput(
            width: 300,
            label: '邮编',
            value: resPartner.zip,
            onSaved: (v) {
              resPartner.zip = v;
            },
          ),
          CryInput(
            width: 300,
            label: '区域',
            value: resPartner.area,
            onSaved: (v) {
              resPartner.area = v;
            },
          ),
          CrySelect(
            label: '*国家',
            dataList: countryIdList,
            value: resPartner.countryId,
            onSaved: (v) {
              resPartner.countryId = v;
            },
            validator: (v) {
              return v!.isEmpty ? S.of(context).required : null;
            },
          ),
          CryInput(
            width: 300,
            label: '城市',
            value: resPartner.city,
            onSaved: (v) {
              resPartner.city = v;
            },
          ),
          CryInput(
            width: 300,
            label: '地址',
            value: resPartner.contactAddress,
            onSaved: (v) {
              resPartner.contactAddress = v;
            },
          ),
          CryInput(
            width: 300,
            label: '主页',
            value: resPartner.website,
            onSaved: (v) {
              resPartner.website = v;
            },
          ),
          CryInput(
            width: 300,
            label: '中天业务经理',
            value: resPartner.userName,
            onTap: () => selectUser(),
            onSaved: (v) {
              resPartner.userName = v;
            },
          ),
          CryInput(
            width: 300,
            label: '开户银行',
            value: resPartner.bank,
            onSaved: (v) {
              resPartner.bank = v;
            },
          ),
          CryInput(
            width: 300,
            label: '银行账号',
            value: resPartner.bankAccount,
            onSaved: (v) {
              resPartner.bankAccount = v;
            },
          ),
          CryInput(
            width: 300,
            label: '税号',
            value: resPartner.vat,
            onSaved: (v) {
              resPartner.vat = v;
            },
          ),
          CryInput(
            width: 300,
            label: '联系电话',
            value: resPartner.phone,
            onSaved: (v) {
              resPartner.phone = v;
            },
          ),
          CryInput(
            width: 300,
            label: '备注',
            value: resPartner.comment,
            onSaved: (v) {
              resPartner.comment = v;
            },
          ),
        ],
      ),
    );
    var buttonBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: S.of(context).save,
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            // DynamicDataApi.getModels(model: "dms.documenttype",key: "documenttypecode",val: resPartner.documenttypecode)
            // .then((ResponseBodyApi res) {
            //   if (!res.success!) {
            //     // CryUtils.message("");
            //     return;
            //   }
            //   if(res.data.length != 0){
            //     CryUtils.message("该项目代码已存在");
            //     return;
            //   }
            ResPartnerOdooApi.saveOrUpdate(resPartner.toOdooMap())
                .then((ResponseBodyApi res) {
              if (!res.success!) {
                //CryUtils.message(re s.message!);
                return;
              }
              Navigator.pop(context, true);
              CryUtils.message(S.of(context).saved);
            });
            // });
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      appBar: AppBar(
        title: Text(this.isAdd! ? S.of(context).add : S.of(context).modify),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: buttonBar,
    );
    return SizedBox(
      width: 650,
      height: 650,
      child: result,
    );
    // return result;
  }
}
